<template>
  <div class="app-container">
    <a-form
      :label-col="{ span: 8 }"
      :model="formData"
      :wrapper-col="{ span: 16 }"
      autocomplete="off"
      name="basic"
      @finish="onFinish">
      <a-form-item label="登录用户" name="login_name">
        <a-input v-model:value="formData.login_name" disabled style="width: 260px" />
      </a-form-item>
      <!-- <a-form-item :rules="[{ required: false, message: '请输入账号!' }]" label="云盘账号" name="account">
        <a-input v-model:value="formData.account" style="width: 260px" />
      </a-form-item>
      <a-form-item :rules="[{ required: false, message: '请输入密码!' }]" label="云盘密码" name="password">
        <a-input-password v-model:value="formData.password" style="width: 260px" />
      </a-form-item> -->
      <a-form-item :rules="[{ required: true, message: '请选择!' }]" label="执行状态">
        <a-radio-group v-model:value="formData.is_execute">
          <a-radio :value="0">暂停</a-radio>
          <a-radio :value="1">执行</a-radio>
        </a-radio-group>
      </a-form-item>

      <a-form-item :rules="[{ required: true, message: '请输入!' }]" label="执行间隔时间">
        <a-input-number v-model:value="formData.time_interval" addon-after="分钟" />
      </a-form-item>
      <a-form-item label="钉钉机器人token" name="ding_talk_token">
        <a-input v-model:value="formData.ding_talk_token" style="width: 260px" />
      </a-form-item>
      <a-form-item label="tg_bot_token" name="tg_bot_token">
        <a-input v-model:value="formData.tg_bot_token" style="width: 260px" />
      </a-form-item>
      <a-form-item label="tg_chat_id" name="tg_chat_id">
        <a-input v-model:value="formData.tg_chat_id" style="width: 260px" />
      </a-form-item>
      <a-form-item :wrapper-col="{ offset: 8, span: 16 }">
        <a-button html-type="submit" type="primary">保存</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>
<script setup>
  import { useCloudDiskStore } from '@/stores/CloudDisk.js'
  import TaskAPI from '@/api/cluodtask.js'
  import { onMounted, ref, watch } from 'vue'
  import { message } from 'ant-design-vue'

  const cloudDiskStore = useCloudDiskStore()
  const formData = ref({
    login_name: '',
    password: '',
    account: '',
    time_interval: '',
    is_execute: 0,
    ding_talk_token: '',
    tg_bot_token: '',
    tg_chat_id: '',
  })

  const getInfo = () => {
    TaskAPI.getUserInfo({ login_name: cloudDiskStore.CloudDiskUserInfo.loginName }).then((res) => {
      const { login_name, password, account, time_interval, is_execute, ding_talk_token, tg_bot_token, tg_chat_id } =
        res[0]
      formData.value = {
        login_name,
        password,
        account,
        time_interval,
        is_execute,
        ding_talk_token,
        tg_bot_token,
        tg_chat_id,
      }
    })
  }
  const onFinish = (values) => {
    // console.log('Success:', values)
    TaskAPI.updateUserInfo(formData.value).then((res) => {
      console.log('Success:', res)
      message.success('保存成功')
      TaskAPI.createTimedTask({ login_name: cloudDiskStore.CloudDiskUserInfo.loginName })
    })
  }
  watch(
    () => cloudDiskStore.CloudDiskUserInfo.loginName,
    (loginName) => {
      if (loginName) getInfo()
    },
    { immediate: true, deep: true },
  )
  onMounted(() => {
    // getInfo()
  })
</script>
<style lang="less" scoped></style>
